<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>observableset.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_labs_observe_observableset.js.html">observableset.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A set of {@code goog.labs.observe.Observable}s that
<a name="line17"></a> * allow registering and removing observers to all of the observables
<a name="line18"></a> * in the set.
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.labs.observe.ObservableSet&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.array&#39;);
<a name="line24"></a>goog.require(&#39;goog.labs.observe.Observer&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Creates a set of observables.
<a name="line30"></a> *
<a name="line31"></a> * An ObservableSet is a collection of observables. Observers may be
<a name="line32"></a> * reigstered and will receive notifications when any of the
<a name="line33"></a> * observables notify. This class is meant to simplify management of
<a name="line34"></a> * observations on multiple observables of the same nature.
<a name="line35"></a> *
<a name="line36"></a> * @constructor
<a name="line37"></a> */
<a name="line38"></a>goog.labs.observe.ObservableSet = function() {
<a name="line39"></a>  /**
<a name="line40"></a>   * The observers registered with this set.
<a name="line41"></a>   * @type {!Array.&lt;!goog.labs.observe.Observer&gt;}
<a name="line42"></a>   * @private
<a name="line43"></a>   */
<a name="line44"></a>  this.observers_ = [];
<a name="line45"></a>
<a name="line46"></a>  /**
<a name="line47"></a>   * The observables in this set.
<a name="line48"></a>   * @type {!Array.&lt;!goog.labs.observe.Observable&gt;}
<a name="line49"></a>   * @private
<a name="line50"></a>   */
<a name="line51"></a>  this.observables_ = [];
<a name="line52"></a>};
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Adds an observer that observes all observables in the set. If new
<a name="line57"></a> * observables are added to or removed from the set, the observer will
<a name="line58"></a> * be registered or unregistered accordingly.
<a name="line59"></a> *
<a name="line60"></a> * The observer will not be added if there is already an equivalent
<a name="line61"></a> * observer.
<a name="line62"></a> *
<a name="line63"></a> * @param {!goog.labs.observe.Observer} observer The observer to invoke.
<a name="line64"></a> * @return {boolean} Whether the observer is actually added.
<a name="line65"></a> */
<a name="line66"></a>goog.labs.observe.ObservableSet.prototype.addObserver = function(observer) {
<a name="line67"></a>  // Check whether the observer already exists.
<a name="line68"></a>  if (goog.array.find(this.observers_, goog.partial(
<a name="line69"></a>          goog.labs.observe.Observer.equals, observer))) {
<a name="line70"></a>    return false;
<a name="line71"></a>  }
<a name="line72"></a>
<a name="line73"></a>  this.observers_.push(observer);
<a name="line74"></a>  goog.array.forEach(this.observables_, function(o) {
<a name="line75"></a>    o.observe(observer);
<a name="line76"></a>  });
<a name="line77"></a>  return true;
<a name="line78"></a>};
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Removes an observer from the set. The observer will be removed from
<a name="line83"></a> * all observables in the set. Does nothing if the observer is not in
<a name="line84"></a> * the set.
<a name="line85"></a> * @param {!goog.labs.observe.Observer} observer The observer to remove.
<a name="line86"></a> * @return {boolean} Whether the observer is actually removed.
<a name="line87"></a> */
<a name="line88"></a>goog.labs.observe.ObservableSet.prototype.removeObserver = function(observer) {
<a name="line89"></a>  // Check that the observer exists before removing.
<a name="line90"></a>  var removed = goog.array.removeIf(this.observers_, goog.partial(
<a name="line91"></a>      goog.labs.observe.Observer.equals, observer));
<a name="line92"></a>
<a name="line93"></a>  if (removed) {
<a name="line94"></a>    goog.array.forEach(this.observables_, function(o) {
<a name="line95"></a>      o.unobserve(observer);
<a name="line96"></a>    });
<a name="line97"></a>  }
<a name="line98"></a>  return removed;
<a name="line99"></a>};
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * Removes all registered observers.
<a name="line104"></a> */
<a name="line105"></a>goog.labs.observe.ObservableSet.prototype.removeAllObservers = function() {
<a name="line106"></a>  this.unregisterAll_();
<a name="line107"></a>  this.observers_.length = 0;
<a name="line108"></a>};
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * Adds an observable to the set. All previously added and future
<a name="line113"></a> * observers will be added to the new observable as well.
<a name="line114"></a> *
<a name="line115"></a> * The observable will not be added if it is already registered in the
<a name="line116"></a> * set.
<a name="line117"></a> *
<a name="line118"></a> * @param {!goog.labs.observe.Observable} observable The observable to add.
<a name="line119"></a> * @return {boolean} Whether the observable is actually added.
<a name="line120"></a> */
<a name="line121"></a>goog.labs.observe.ObservableSet.prototype.addObservable = function(observable) {
<a name="line122"></a>  if (goog.array.contains(this.observables_, observable)) {
<a name="line123"></a>    return false;
<a name="line124"></a>  }
<a name="line125"></a>
<a name="line126"></a>  this.observables_.push(observable);
<a name="line127"></a>  goog.array.forEach(this.observers_, function(observer) {
<a name="line128"></a>    observable.observe(observer);
<a name="line129"></a>  });
<a name="line130"></a>  return true;
<a name="line131"></a>};
<a name="line132"></a>
<a name="line133"></a>
<a name="line134"></a>/**
<a name="line135"></a> * Removes an observable from the set. All observers registered on the
<a name="line136"></a> * set will be removed from the observable as well.
<a name="line137"></a> * @param {!goog.labs.observe.Observable} observable The observable to remove.
<a name="line138"></a> * @return {boolean} Whether the observable is actually removed.
<a name="line139"></a> */
<a name="line140"></a>goog.labs.observe.ObservableSet.prototype.removeObservable = function(
<a name="line141"></a>    observable) {
<a name="line142"></a>  var removed = goog.array.remove(this.observables_, observable);
<a name="line143"></a>  if (removed) {
<a name="line144"></a>    goog.array.forEach(this.observers_, function(observer) {
<a name="line145"></a>      observable.unobserve(observer);
<a name="line146"></a>    });
<a name="line147"></a>  }
<a name="line148"></a>  return removed;
<a name="line149"></a>};
<a name="line150"></a>
<a name="line151"></a>
<a name="line152"></a>/**
<a name="line153"></a> * Removes all registered observables.
<a name="line154"></a> */
<a name="line155"></a>goog.labs.observe.ObservableSet.prototype.removeAllObservables = function() {
<a name="line156"></a>  this.unregisterAll_();
<a name="line157"></a>  this.observables_.length = 0;
<a name="line158"></a>};
<a name="line159"></a>
<a name="line160"></a>
<a name="line161"></a>/**
<a name="line162"></a> * Removes all registered observations and observables.
<a name="line163"></a> */
<a name="line164"></a>goog.labs.observe.ObservableSet.prototype.removeAll = function() {
<a name="line165"></a>  this.removeAllObservers();
<a name="line166"></a>  this.observables_.length = 0;
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * Unregisters all registered observers from all registered observables.
<a name="line172"></a> * @private
<a name="line173"></a> */
<a name="line174"></a>goog.labs.observe.ObservableSet.prototype.unregisterAll_ = function() {
<a name="line175"></a>  goog.array.forEach(this.observers_, function(observer) {
<a name="line176"></a>    goog.array.forEach(this.observables_, function(o) {
<a name="line177"></a>      o.unobserve(observer);
<a name="line178"></a>    });
<a name="line179"></a>  }, this);
<a name="line180"></a>};
</pre>


</body>
</html>
